-
Notifications
You must be signed in to change notification settings - Fork 324
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New "*endpoint" decorators for custom endpoint creations #964
New "*endpoint" decorators for custom endpoint creations #964
Conversation
DECORATORS |
Suggested by @pieroit |
This is cooking up gooooooood 😍 |
How to reproduce: - Launch the Cat - Goto /docs page - Activate a plugin with a custom endpoint - The custom endpoint can be used - Refresh page /docs - The new custom endpoint is not there Why: The openapi schema is cached by get_openapi_configuration_function Solution: Each time a function is decorated with `endpoint`, we flush the cache
- Routes manually removed from FastAPI routes (seems there is no official method to do this) - Flush the docs cache
Why not using plugin.deactivate()? the "endpoint" decorator needs the reference to the FastAPI app instance for endpoint removing. Calling plugin.deactivate() the method CustomEndpoint.deactivate() raises the exception: "self.cheshire_cat_api is None" because the method on_finish_plugins_sync_callback is not called so the cheshire_cat_api is not injected
@sambarza yes awesome |
Thanks @sambarza had a great experience reviewing
I'll spend some more time to see if the Awesome |
Description
New decorators to permit the creation of custom endpoints:
Type of change
Checklist:
Simple example of use inside a plugin
Consume with:
curl http://localhost:1865/custom/hello
More complex example
Consume with:
Internals
The decorators wrap the function as a FastAPI path operation, all the FastAPI path operation parameters can be used.
The default prefix is
custom
and default tag isCustom Endpoints
The new decorators are managed as other decorators (approach was: searched everywhere for
hook
and adapted forendpoint
).Activation and deactivation of plugins
/docs
/docs
tooChange to startup process
Note, there is a change in the CheshireCat startup process, now the CheshireCat needs the FastAPI app instance, why?
The
endpoint
decorator creates a new FastAPI router for each new custom endpoint and then includes the router in the FastAPI app, CheshireCat activate the decorator injecting the FastAPI app.Flows
Startup process
MadHatter.find_plugins()
calls the CheshireCat callbackon_finish_plugins_sync_callback
Endpoint.activate()
for each custom endpointEndpoint.activate()
add the route to the FastAPI routes and clear the/docs
cachePlugin installation process
toggle_plugin()
toggle_plugin()
activates the plugin (however the endpoints are not really active, are not yet included in FastAPI app)toggle_plugin()
calls the CheshireCat callbackon_finish_plugins_sync_callback
Endpoint.activate()
for each custom endpointEndpoint.activate()
add the route to the FastAPI routes and clear the/docs
cachePlugin activation process
toggle_plugin()
activates the plugin (however the endpoints are not really active, are not yet included in FastAPI app)toggle_plugin()
calls the CheshireCat callbackon_finish_plugins_sync_callback
Endpoint.activate()
for each custom endpointEndpoint.activate()
add the route to the FastAPI routes and clear the/docs
cachePlugin deactivation process
toggle_plugin()
deactivate the pluginPlugin.deactivate()
callsEndpoint.deactivate()
for each custom endpoint related to the pluginEndpoint.deactivate()
removes the route from the FastAPI routes and clear the/docs
cachetoggle_plugin()
calls the CheshireCat callbackon_finish_plugins_sync_callback
Plugins API
The endpoints are already exposed by the
/plugins/
API (can be shown in the admin portal).This is an example of the returned payload with the new
endpoints
node:Automatic tests
test_endpoints.py
tests the new decorators.